package org.golang.godev.editors;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

public class GoSourceViewerConfiguration extends SourceViewerConfiguration {
	private GoDoubleClickStrategy doubleClickStrategy ;
	private GoScanner scanner;
	private ColorManager colorManager;

	public GoSourceViewerConfiguration(ColorManager colorManager) {
		this.colorManager = colorManager;
	}
	
	@Override
	public int getTabWidth(ISourceViewer sourceViewer) {
		return 4 ;
	}
	
	public ITextDoubleClickStrategy getDoubleClickStrategy(
		ISourceViewer sourceViewer,
		String contentType) {
		if (doubleClickStrategy == null)
			doubleClickStrategy = new GoDoubleClickStrategy();
		return doubleClickStrategy;
	}

	protected GoScanner getGoScanner() {
		if (scanner == null) {
			scanner = new GoScanner(colorManager);
			scanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(ColorManager.DEFAULT))));
		}
		return scanner;
	}
	
	@Override
	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getGoScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

		return reconciler;
	}

}